<?php

namespace Framework;

use PDO;

class Database {
    public $conn ;

    /**
     * @throws Exception
     */
    public function  __construct($config){
        $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['dbname']}";

        $options = [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
        ];

        try {
            $this->conn = new PDO($dsn, $config['username'], $config['password'],$options);
        } catch (\PDOException $e) {
            throw new \Exception("数据库连接失败:{$e->getMessage()}");
        }
    }

    /**
     * 数据库请求方法
     *
     * @param $query
     * @return false|PDOStatement
     * @throws Exception
     */
    public function query($query, $params = []) {
        try {
            $sth = $this->conn->prepare($query);

            foreach ($params as $param => $value) {
                // 绑定参数到啊查询语句
                $sth->bindValue(':' . $param, $value);
            }


            $sth->execute();
            return $sth;
        }
        catch (\PDOException $e) {
            inspect($params);
            inspect($sth);
            throw new \Exception("数据库请求执行失败: {$e->getMessage()}");
        }
    }

}